package com.lx.oms.controller.service.impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lx.oms.common.constant.MsgConstant;
import com.lx.oms.common.entity.Query;
import com.lx.oms.common.entity.R;
import com.lx.oms.common.utils.CommonUtils;
import com.lx.oms.controller.entity.SupplyCategoryEntity;
import com.lx.oms.controller.manager.SupplyCategoryManager;
import com.lx.oms.controller.service.SupplyCategoryService;

/**
 * 黄页分类
 *
 * @author houzhong
 * @date 2018年11月22日 上午10:10:19
 */
@Service("supplyCategoryService")
public class SupplyCategoryServiceImpl implements SupplyCategoryService {

	@Autowired
	private SupplyCategoryManager supplyCategoryManager;

	@Override
	public List<SupplyCategoryEntity> listCategoryByParentId(String categoryId) {
		Query query = new Query();
		query.put("parentId", categoryId);
		List<SupplyCategoryEntity> areas = supplyCategoryManager.listCategoryByParentId(query);
		for(SupplyCategoryEntity area : areas) {
			area.checkParent();
		}
		return areas;
	}
	
	@Override
	public R listCategoryByParentId(Map<String, Object> params) {
		Query query = new Query(params);
		List<SupplyCategoryEntity> areas = supplyCategoryManager.listCategoryByParentId(query);
		return CommonUtils.msg(areas);
	}

	@Override
	public R saveSupplyCategory(SupplyCategoryEntity role) {
		int count = supplyCategoryManager.saveSupplyCategory(role);
		return CommonUtils.msg(count);
	}

	@Override
	public R getSupplyCategoryById(BigInteger id) {
		SupplyCategoryEntity supplyCategory = supplyCategoryManager.getSupplyCategoryById(id);
		supplyCategory.checkParentName();
		return CommonUtils.msg(supplyCategory);
	}

	@Override
	public R updateSupplyCategory(SupplyCategoryEntity supplyCategory) {
		int count = supplyCategoryManager.updateSupplyCategory(supplyCategory);
		return CommonUtils.msg(count);
	}

	@Override
	public R batchRemove(String[] id) {
		boolean children = supplyCategoryManager.hasChildren(id);
		if(children) {
			return R.error(MsgConstant.MSG_HAS_CHILD);
		}
		int count = supplyCategoryManager.batchRemove(id);
		return CommonUtils.msg(id, count);
	}
	
	@Override
	public SupplyCategoryEntity getCategoryByName(String name) {
		Query query = new Query();
		query.put("name", name);
		SupplyCategoryEntity categoryEntity = supplyCategoryManager.getCategoryByName(query);
		return categoryEntity;
	}

	@Override
	public List<SupplyCategoryEntity> getCategoryByParentId(String parentId,String type) {
		Query query = new Query();
		query.put("parentId", parentId);
		query.put("type", type);
		
		List<SupplyCategoryEntity> categoryEntity = supplyCategoryManager.getCategoryByParentId(query);
		return categoryEntity;
	}

}
